Skip to content

Add additional version of Resources #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

cpvandehey
Copy link

It would be really useful to have a Resource type that is capable of having a sync init function with an async shutdown.

There are many scenarios where this is useful

Ex:

  1. Create an aiohttp client.
    session = aiohttp.ClientSession()

  2. Before closing the app, I want to await any hanging connections
    await session.close()

@cpvandehey cpvandehey changed the title Update resources.py Add additional version of Resources Oct 26, 2023
@ZipFile
Copy link
Contributor

ZipFile commented Jan 12, 2025

I do not really think we need specific variations of AsyncResource (sync init + async shutdown, async init + sync shutdown) because the way how async stuff works in Python. async def function that never awaits have performance of the regular generator (see docs on coroutines), since it never switches to the event loop.

@ZipFile
Copy link
Contributor

ZipFile commented Jun 1, 2025

Our Resource providers are context-manager-aware now (since next release), so something-like this is now possible:

class Container(containers.DeclarativeContainer):
    my_http_client = providers.Resource(aiohttp.ClientSession, base_url="http://example.com/foo/")

Workarounds like proposed AsyncShutdownResource are no longer needed, so closing this PR.

@ZipFile ZipFile closed this Jun 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants